//
//  AppDelegate+DTCAppService.m
//  BluetoothProject
//
//  Created by JianWei Chen on 16/9/5.
//  Copyright © 2016年 JianWei Chen. All rights reserved.
//

#import "AppDelegate+DTCAppService.h"
#import <KSCrash/KSCrashInstallationStandard.h>

@interface AppDelegate ()<DTCloudOperationDelegate>

@end

@implementation AppDelegate (DTCAppService)

- (void)setDTCloudConfig
{
    [[DTCloudManager defaultJNI_iOS_SDK]setSanBox:YES];
    [[DTCloudManager defaultJNI_iOS_SDK]setOperationDelegate:self];
    [[DTCloudManager defaultJNI_iOS_SDK]startAppId:DTCloudKitAppId appKey:DTCloudKitAppKey successCallback:nil errorCallback:nil];
}

-(void)applicationDidEnterBackground:(UIApplication *)application{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    application.applicationIconBadgeNumber = 0;
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"通知推送错误为: %@", error);
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [application registerForRemoteNotifications];
}

- (void)setNetworkBaseConfig
{
    //网络
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
    config.baseUrl = ServerURL;
}

- (void)setKSCrashConfig
{
    KSCrashInstallationStandard* installation = [KSCrashInstallationStandard sharedInstance];
    installation.url = [NSURL URLWithString:@"https://collector.bughd.com/kscrash?key=050e4c6d7a4de971d0d32116fb65b739"];
    [installation install];
    [installation sendAllReportsWithCompletion:nil];
}

- (void)setWindowBaseConfig
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [[IQKeyboardManager sharedManager]setEnable:YES];
    [[IQKeyboardManager sharedManager]setKeyboardDistanceFromTextField:0];
}

#pragma mark - 代理
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
/**
 *  DtCloud收到设备的命令
 *
 *  @param data  设备命令
 *  @param macAddress 设备的标识
 */
- (void)dtCloudManagerMessageReceive:(NSData *)data macAddress:(NSString *)macAddress
{
    
}
/**
 *  发送到DtCloud命令失败
 *
 *  @param cmdtag 失败的命令标识
 */
- (void)dtCloudManagerMessageSendFaild:(NSUInteger)cmdtag
{
    
}
/**
 *  DtCloud发送的设备上线下线的回调
 *
 *  @param isonline 上线下线
 *  @param macAddress      设备的标识
 */
- (void)dtCloudManagerBackDeviceState:(BOOL)isonline macAddress:(NSString *)macAddress
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DeviceChangeOnlineNotification" object:@{@"onlie":@(isonline),
                                                                                                         @"macAddress":macAddress}];
}

/**
 *  DTCloud收到设备的命令
 *
 *  @param backCode    返回状态码
 *  @param backContent 返回内容
 *  @param macAddress  设备的标识
 */
- (void)dtCloudManagerMessageReceiveBackCode:(NSString *)backCode backContent:(NSString *)backContent macAddress:(NSString *)macAddress
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DeviceStateNotification" object:backContent userInfo:@{@"backCode":backCode,@"macAddress":[self toUpper:macAddress]}];
}

- (void)dtCloudManagerDeviceMatchingNetworkStage:(NSString *)stageString
{
    [[AlertViewManager shareManager]initWithMessage:stageString cancleButtonTitle:@"确定" clickAtIndex:^(NSInteger buttonIndex, UIAlertView *alertView) {

    }];
}

// macAddress转大写
-(NSString *)toUpper:(NSString *)str{
    for (NSInteger i=0; i<str.length; i++) {
        if ([str characterAtIndex:i]>='a'&[str characterAtIndex:i]<='z') {
            //A  65  a  97
            char  temp=[str characterAtIndex:i]-32;
            NSRange range=NSMakeRange(i, 1);
            str=[str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
        }
    }
    return str;
}
#pragma clang diagnostic pop

@end
